package array;


/**
 * Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.
 * <p>
 * Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
 * <p>
 * Simple
 * Given nums = [1,1,2],
 * <p>
 * Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
 * It doesn't matter what you leave beyond the new length.
 *
 * @author KoizumiSinya
 * @date 2018/2/6. 22:30
 * @editor
 * @date
 * @describe
 */
public class Day1 {

    public static int myTest(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }

        int index = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] != array[index]) {
                array[++index] = array[i];
            }
        }

        //+1的原因是因为角标从0开始计算，当最后剔除重复的时候，需要把第一个0位置的元素加上
        return index + 1;
    }

    /**
     * Time complexity O(n)
     * Space complexity O(1)
     * @param nums
     * @return
     */
    public static int bestTest(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        int i = 0;
        for (int j = 0; j < nums.length; j++) {
            if (nums[j] != nums[i]) {
                i++;
                nums[i] = nums[j];
            }
        }
        return i + 1;
    }


}
